package com.ysstech.kettle.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JsonUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.kettle.entity.JobTask;
import com.ysstech.kettle.entity.JobTaskDetails;
import com.ysstech.kettle.service.JobRelationService;
import com.ysstech.kettle.service.JobTaskDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-11
 */
@Controller
@Slf4j
@Api(tags = "作业任务明细api")
@RequestMapping("/jobTaskDetails")
public class JobTaskDetailsController extends BaseController {
    @Autowired
    private JobTaskDetailsService jobTaskDetailsService;
    @Autowired
    private JobRelationService jobRelationService;

    /**
     * 获取作业作业任务明细
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:task"})
    @ApiOperation(value = "获取作业任务明细信息")
    @YssLog(module = "作业任务明细管理", remark = "获取作业任务明细信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(String taskId) throws Exception {
        return ResultGenerator.getSuccessResult("获取作业任务明细信息成功",
                jobTaskDetailsService.listInfo(taskId));
    }


    /**
     * 新增作业任务明细明细的方法
     *
     * @param jobTaskDetails
     * @return
     */
    @YssPermissions(value = {"kettle:job:task:add"})
    @ApiOperation(value = "新增作业任务明细信息")
    @YssLog(module = "作业任务明细管理", remark = "新增作业任务明细信息")
    @RequestMapping(value = "/addJobTaskDetails")
    @ResponseBody
    public RestResult addJobTaskDetails(JobTaskDetails jobTaskDetails) throws Exception {
        jobTaskDetails.setId(DateUtil.generateUUID());
        jobTaskDetails.setAddTime(LocalDateTime.now());
        jobTaskDetailsService.save(jobTaskDetails);
        JobTask jobTask = new JobTask();
        jobTask.setId(jobTaskDetails.getTaskId());
        jobRelationService.insertJobRelation(jobTask);
        return ResultGenerator.getSuccessResult("新增作业任务明细信息成功！");
    }

    /**
     * 修改作业任务明细信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:task:edit"})
    @ApiOperation(value = "修改作业任务明细信息")
    @YssLog(module = "作业任务明细管理", remark = "修改作业任务明细信息")
    @RequestMapping(value = "/editJobTaskDetails")
    @ResponseBody
    public RestResult editJobTaskDetails(JobTaskDetails jobTaskDetails) throws Exception {
        if (jobTaskDetails == null || StringUtils.isBlank(jobTaskDetails.getId())) {
            throw new BusinessException("获取作业任务明细信息失败，请稍后重试！");
        }
        jobTaskDetails.setEditTime(LocalDateTime.now());
        jobTaskDetailsService.updateById(jobTaskDetails);
        JobTask jobTask = new JobTask();
        jobTask.setId(jobTaskDetails.getTaskId());
        jobRelationService.insertJobRelation(jobTask);
        return ResultGenerator.getSuccessResult("修改作业任务明细信息成功！");
    }

    /**
     * 删除作业任务明细信息
     *
     * @return
     */
    @YssPermissions(value = {"kettle:job:task:del"})
    @ApiOperation(value = "删除作业任务明细信息")
    @YssLog(module = "作业任务明细管理", remark = "删除作业任务明细信息")
    @RequestMapping(value = "/delJobTaskDetails")
    @ResponseBody
    public RestResult delJobTaskDetails(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除作业任务明细信息失败，请稍后重试！");
        }
        List<String> list = JSONArray.parseArray(ids, String.class);
        if (CollectionUtils.isEmpty(list)) {
            throw new BusinessException("获取删除作业任务明细信息失败，请稍后重试！");
        }
        //查询对应的任务信息
        QueryWrapper<JobTaskDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("ID", list.get(0));
        JobTaskDetails jobTaskDetails = jobTaskDetailsService.getOne(wrapper);
        if (null != jobTaskDetails && StringUtils.isNotBlank(jobTaskDetails.getTaskId())) {
            JobTask jobTask = new JobTask();
            jobTask.setId(jobTaskDetails.getTaskId());
            jobRelationService.insertJobRelation(jobTask);
        }
        jobTaskDetailsService.removeByIds(list);
        return ResultGenerator.getSuccessResult("删除作业任务明细信息成功！");
    }

}
